package com.zzyk.web.controller;


import com.github.pagehelper.PageInfo;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.controller.BaseController;
import com.zzyk.main.model.pojo.AlarmMsgLog;
import com.zzyk.main.model.pojo.CommonCount;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.service.AlarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wss_PC
 * @DESCRIPTION 报警信息控制器
 * @create 2020-6-15 15:55
 */
@RestController
@RequestMapping("/alarm")
public class AlarmController extends BaseController {
    @Autowired
    private AlarmInfoService alarmInfoService;

    /**
     * 多添加查询消息推送记录
     * @param eId
     * @param beginTime
     * @param endTime
     * @param status
     * @return
     */
    @GetMapping("/findAlarmMsgLog")
    public Message findAlarmMsgLog(Long eId, String beginTime, String endTime,  String status,Integer type) throws UserException {
        SysUser user = currentUser();
        String[] areas =  user.getArea();
    try {
        setPage();
        PageInfo<AlarmMsgLog> info =new PageInfo<>(alarmInfoService.selectAlarmMsgLog(eId,beginTime,endTime,status,areas,type));
        return  Message.success()
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }catch (Exception e){
        e.printStackTrace();
        return Message.failed("查询失败");
    }
    }
    /**
     * 多条件查询报警信息
     * @param eId
     * @param dId
     * @param beginTime
     * @param endTime
     * @param status
     * @return
     */
    @GetMapping("/findAlarmByParams")
    public Message findAlarmByParams(Long eId, Long dId, String beginTime, String endTime, Long status, Integer pageIndex, Integer pageSize) throws UserException {
        setPage();
        SysUser user = currentUser();
        String[] areas =  user.getArea();
        Integer[] deviceList = user.getDeviceList();
        try {
            return alarmInfoService.selectAlarmByParams(eId,dId,beginTime,endTime,status,areas,pageIndex,pageSize,deviceList);
        }catch (Exception e){
            e.printStackTrace();
            return  Message.failed("查询失败");
        }
    }

    /**
     * 统计各个报警级别的数量
     * @return
     */
    @GetMapping("/findCountAlarm")
    public List<CommonCount> findCountAlarm(Long eId, Long dId, String beginTime, String endTime, Long status) throws UserException {
        SysUser user = currentUser();
        String[] areas = user.getArea();
        Integer[] deviceList = user.getDeviceList();
        return alarmInfoService.findCountAlarm(null, areas,eId,dId,beginTime,endTime,status,deviceList);
    }
}